package com.demo.index;

import com.alibaba.fastjson.JSONObject;
import com.demo.common.model.Cart;
import com.demo.common.model.User;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.kit.HttpKit;

import java.io.BufferedReader;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * IndexController
 */
@Before(IndexInterceptor.class)
public class IndexController extends Controller {

	@Inject
	IndexService service;

	public void index() {
		render("index.html");
	}


	//登录
	public void login() throws Exception {
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
        String json = HttpKit.readData(getRequest());
        User u = FastJson.getJson().parse(json, User.class);
		//User u=getRequestObject(User.class);
		//String tel=getPara("tel");
		//String password=getPara("password");
		//User user=service.sign(tel,password);
		User user=service.sign(u.getTel(),u.getPassword());
		JSONObject jsonObject=new JSONObject();
		if(user!=null) {
			jsonObject.put("result","1");
			jsonObject.put("user",user);
			renderJson(jsonObject);
		}else{
			jsonObject.put("result","2");
			renderJson(jsonObject);
		}
	}

    //注册
	public void register(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		String json = HttpKit.readData(getRequest());
        User u = FastJson.getJson().parse(json, User.class);
//		String email=getPara("email");
//		String password=getPara("password");
		User user=new User();
		user.setEmail(u.getTel());
		user.setPassword(u.getPassword());
		user.save();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result","1");
		renderJson(jsonObject);
	}

	//查询个人信息
	public void findUserById(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		User user=service.findUserById(getParaToInt());
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result","1");
		jsonObject.put("user",user);
		renderJson(jsonObject);
	}

	//修改个人信息
	public void updateUserById(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		User user=service.findUserById(getParaToInt());
		user.setName(getPara("name")==null?getPara("name"):"");
		user.setBirthday(getPara("birthday")==null?getPara("birthday"):"");
		user.setSex(getPara("sex")==null?getPara("sex"):"");
		user.setArea(getPara("area")==null?getPara("area"):"");
		user.update();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result","1");
		renderJson(jsonObject);
	}

	//购物车列表
	public void findCartList(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result","1");
		jsonObject.put("cart",service.cartlist());
		renderJson(jsonObject);
	}

    //删除购物车订单
	public void deleteCartById(){
		getResponse().addHeader("Access-Control-Allow-Origin", "*");
		Cart cart=service.findCartById(getParaToInt());
		cart.setVisble(1);
		cart.update();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result","1");
		renderJson(jsonObject);
	}

	/**
	 * 取Request中的数据对象
	 * @param valueType
	 * @return
	 * @throws Exception
	 */
	protected <T> T getRequestObject(Class<T> valueType) throws Exception {
		StringBuilder json = new StringBuilder();
		BufferedReader reader = this.getRequest().getReader();
		String line = null;
		while((line = reader.readLine()) != null){
			json.append(line);
		}
		reader.close();

		return JSONObject.parseObject(json.toString(), valueType);
	}


}



